import cv2
import os
from tqdm import tqdm
blockSize=32
block = 256 // blockSize



def create_mask(imgpath):
    image = cv2.imread(imgpath, cv2.IMREAD_GRAYSCALE)
    _, mask = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)
    return mask


def getcolor(image):
    height = image.shape[0]
    weight = image.shape[1]

    maxColor = 0
    dictColor =[0]*block*block*block


    for row in tqdm(range(height)):  # 遍历高

        for col in range(weight):  # 遍历宽
            blue = image[row, col, 0]
            green = image[row, col, 1]
            red = image[row, col, 2]
            blue//=blockSize
            green//=blockSize
            red//=blockSize

            countColor = blue + green * block + red * block * block

            dictColor[countColor] = dictColor[countColor] + 1
            if dictColor[countColor] > dictColor[maxColor]:
                maxColor = countColor

    return maxColor




if __name__ == '__main__':
    rootpath = "1"
    masksavepath = "mask"
    savepath = "result"
    imgfiles = os.listdir(rootpath)
    for i in range(0, len(imgfiles)):
        path = os.path.join(rootpath, imgfiles[i])
        print(imgfiles[i])
        if os.path.isfile(path):
            if (imgfiles[i].endswith("jpg") or imgfiles[i].endswith("JPG")):

                image = cv2.imread(path)

                color = getcolor(image)
                blue=color%block
                green=color//block%block
                red=color//block//block
                print(blue*blockSize,green*blockSize,red*blockSize)
